package com.scau.ums.controller;

import com.scau.ums.model.Info;
import com.scau.ums.model.Lostandfoundinfo;
import com.scau.ums.service.LostAndFoundService;
import com.scau.ums.service.imp.info.DefaultSendInfoImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xfy
 * @version 1.0
 * @date 2019/8/8 15:32
 */

/**
 * 关于审核失物招领的控制类
 */
@Controller
public class VerifyLostAndFoundController {
    @Autowired
    LostAndFoundService service;
    @Autowired
    DefaultSendInfoImp defaultSendInfoImp;
    /**
     * 在管理员失物招领审核页面返回未审核的失物招领信息
     * @return
     */

    @RequestMapping("verifyLostAndFound")
    String getUncheckeLostAndFoundPost(HttpServletRequest request, HttpServletResponse response){
        System.out.println("here");
        List<Lostandfoundinfo> list = service.getLostAndFoundInfoByState("审核中");
        request.setAttribute("lostAndFoundList",list);

        return "verifyLostAndFound";
    }


    @RequestMapping(value = "verifyPassed")
    @ResponseBody
    String verifyPassed(HttpServletRequest request){
        Integer id = Integer.valueOf(request.getParameter("id"));
        String userId = request.getParameter("userId");
        String userType = request.getParameter("userType");
        String itemName = request.getParameter("itemName");
        System.out.println("id:" + id + " userId:" + userId + " userType:" + userType + " itemName:" + itemName);
        try{
            service.verifyPass(id);
        }catch (Exception e){
            return "verifyPassed";
        }
        Info info = defaultSendInfoImp.createInfo("失物招领审核结果", "", userId, userType,"您的失物招领" +
                "（物品名称为：" + itemName + "）审核通过，请查看" );
        defaultSendInfoImp.sendInfo(info);//发送邮件

        return "verifyPassed";
    }


    @RequestMapping(value = "verifyFailed")
    @ResponseBody
    String verifyFailed(HttpServletRequest request){
        Integer id = Integer.valueOf(request.getParameter("id"));
        String userId = request.getParameter("userId");
        String userType = request.getParameter("userType");
        String itemName = request.getParameter("itemName");
        System.out.println("id:" + id + " userId:" + userId + " userType:" + userType + " itemName:" + itemName);
        try{
            service.verifyFail(id);
        }catch (Exception e){
            return "verifyPassed";
        }
        Info info = defaultSendInfoImp.createInfo("失物招领审核结果", "", userId, userType,"您的失物招领" +
                "（物品名称为：" + itemName + "）审核不通过，请检查内容是否合法" );
        defaultSendInfoImp.sendInfo(info);//发送邮件

        return "verifyFailed";
    }

    @RequestMapping("setFounded")
    @ResponseBody
    String setFounded(HttpServletRequest request){
        Lostandfoundinfo lostandfoundinfo = new Lostandfoundinfo();
        try{
            service.setIsFounded(Integer.valueOf(request.getParameter("lostAndFoundId")), request.getParameter("idFounded"));
        }catch (Exception e){
            e.printStackTrace();
            return "faile";
        }
        return "done";
    }

}
